clear
set mem 300m

capture log close
log using "$dataloc/log/gdp", text replace
set more off

/* --------------------------------------------------------------- *
   Author: Ed Al-Hussainy
   Description: GDP series 
 * --------------------------------------------------------------- */

forvalues i = 1/10 { 
    tempfile tmp`i' 
}


/* --------------------------------------------------------------- *
    IFS + WDI + WEO GDP data
 * --------------------------------------------------------------- */

use cncode year value99B using "$dataloc/Data/IFS/$dataver/IFS", clear

rename value99B ifs_gdp_lcu
label var ifs_gdp_lcu "IFS GDP (current LCU)" 

merge cncode year using "$dataloc/output/WDI_WEOmetrics", sort keep(cncode year weo_gdp_lcu wdi_gdp_lcu)
drop _merge
sort cncode
merge cncode using "$dataloc/data/CountryClassification2010", keep(cncode cn region incgr)
keep if _merge == 3
drop _merge 

drop if year > $endyear

do "$codeloc/country_selector"

/* --------------------------------------------------------------- *
    summary stats
 * --------------------------------------------------------------- */

/*
preserve

   foreach i in ifs_gdp_lcu weo_gdp_lcu wdi_gdp_lcu {
      gen l_`i' = ln(`i')
      drop if missing(`i')
   }

   twoway (kdensity l_ifs_gdp_lcu) (kdensity l_weo_gdp_lcu) (kdensity l_wdi_gdp_lcu)
   graph export "$dataloc/output/figs/gdp_kdensity_world.wmf", replace

   levelsof region
   foreach i in r(levels) {

      twoway (kdensity l_ifs_gdp_lcu) (kdensity l_weo_gdp_lcu) (kdensity l_wdi_gdp_lcu), title("`i'")
      graph export "$dataloc/output/figs/`i'_gr_`j'.wmf", replace

   }   
   

   keep cncode  year ifs_cpiY GDP weo_gdp_lcu wdi_gdp_lcu
   gen ifs_weo =  ifs_gdp_lcu/ weo_gdp_lcu
   gen ifs_wdi =  ifs_gdp_lcu/ wdi_gdp_lcu
   gen weo_wdi =  weo_gdp_lcu/ wdi_gdp_lcu
   sum ifs_weo ifs_wdi weo_wdi


restore
*/

/* --------------------------------------------------------------- *
    GDP series splicing
 * --------------------------------------------------------------- */
 
gen gdp_note = ""
replace gdp_note = "IFS" if !missing(ifs_gdp_lcu)

replace ifs_gdp_lcu = . if cncode == "BHS" & year == 1996
replace gdp_note = "WDI" if cncode == "BHS" & year == 1996

gen GDP = ifs_gdp_lcu

bysort cncode: egen _g1 = count(GDP)
tab cn if _g1 == 0
replace GDP = wdi_gdp_lcu if _g1 == 0
replace gdp_note = "WDI" if _g1 == 0 & !missing(wdi_gdp_lcu)

bysort cncode (year): gen gr_weo_gdp_lcu = weo_gdp_lcu/weo_gdp_lcu[_n-1]
bysort cncode (year): gen gr_wdi_gdp_lcu = wdi_gdp_lcu/wdi_gdp_lcu[_n-1]   
bysort cncode (year): gen gr_ifs_gdp_lcu = ifs_gdp_lcu/ifs_gdp_lcu[_n-1]  

bysort cncode (year): replace gdp_note = "Est (WDI gr)" if missing(GDP) & !missing(wdi_gdp_lcu) 
bysort cncode (year): replace gdp_note = "Est (WEO gr)" if missing(GDP) & missing(wdi_gdp_lcu) & !missing(weo_gdp_lcu)


bysort cncode (year): replace GDP = GDP[_n-1] * gr_wdi_gdp_lcu if missing(GDP)
bysort cncode (year): replace GDP = GDP[_n-1] * gr_weo_gdp_lcu if missing(GDP)

gsort cncode -year

by cncode: replace GDP = GDP[_n-1] / gr_wdi_gdp_lcu[_n-1] if missing(GDP)
by cncode: replace GDP = GDP[_n-1] / gr_weo_gdp_lcu[_n-1] if missing(GDP)

replace GDP = weo_gdp_lcu if cncode == "TWN"
replace gdp_note = "WEO" if cncode == "TWN" 

/* --------------------------------------------------------------- *
    save results
 * --------------------------------------------------------------- */
 
label var GDP "GDP (current LCU) IFS/WDI/WEO"

drop if year < $startyear - 1 
drop if year > $endyear

isid cncode year
sort cncode year

compress 

outsheet cncode cn year GDP gdp_note using "$dataloc/output/metadata/GDP_metadata.csv", comma replace

keep cncode year GDP weo_gdp_lcu wdi_gdp_lcu ifs_gdp_lcu 
 
label data "GDP (Version: $dataver)"
save "$dataloc/Output/GDP", replace


/* end of code */
clear
log close
exit